/**
 * DES crypter
 * @version 1.1 2010.03.11
 * @author Johnny Ling
 */
package com.spring.common.util;

import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESCrypter {

	/**
	 * Encrypter 
	 * @param plainStr plain string
	 * @param keyStr encrypt key
	 * @return encrypted string
	 */
    public static String encrypt(String plainStr, String keyStr) {
        String encryptStr = null;
        if (!plainStr.equals("")) {
            try {
                SecureRandom sr = new SecureRandom();     
                DESKeySpec dks = new DESKeySpec(keyStr.getBytes("UTF-8"));        
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
                SecretKey securekey = keyFactory.generateSecret(dks);
                Cipher cipher = Cipher.getInstance("DES");
                cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
                encryptStr = byte2hex(cipher.doFinal(plainStr.getBytes("UTF-8")));
            } catch(Exception e) {
            	e.printStackTrace();
            }
        }
        
        return encryptStr;
    }
    
    /**
     * Decrypter
     * @param encryptedStr encrypted string
     * @param keyStr encrypt key
     * @return plain string
     */
    public static String decrypt(String encryptedStr, String keyStr) {
    	String result = encryptedStr;
        try {
            SecureRandom sr = new SecureRandom();
            DESKeySpec dks = new DESKeySpec(keyStr.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
            result = new String(cipher.doFinal(hex2byte(encryptedStr)));
        } catch(Exception e) {
        	e.printStackTrace();
        }
        
        return result;
    }    

    /** 
     * convert byte to hex
     */  
    static String byte2hex(byte[] b) {
         StringBuffer buffer = new StringBuffer();  
         String stmp = "";  
         for (int n = 0; n < b.length; n++) {  
             stmp = (Integer.toHexString(b[n] & 0XFF)); 
             if (stmp.length() == 1) {
            	 buffer.append("0" + stmp);  
             } else {  
            	 buffer.append(stmp);
             }
         }  
         return new String(buffer).toLowerCase();
     }
     
     /** 
      * convert string to byte
      */
     static byte[] hex2byte(String str) {
    	 byte[] result = null;
		 try {
			 byte[] b = str.getBytes("UTF-8");
			if((b.length % 2) != 0) {
	             throw new IllegalArgumentException("Invalid hex string");
	         }
	         result = new byte[b.length / 2];  
	         for (int n = 0; n < b.length; n += 2) {  
	             String item = new String(b,n,2);  
	             result[n/2] = (byte)Integer.parseInt(item,16);  
	         }
		 } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		 }
           
         
         return result;
     } 
    
}
